package code.whmmm.util.spring.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * spring request 工具类
 * <br/><br/>
 * 创建时间: 2020/1/7 10:15
 *
 * @author whmmm
 */
public class SpringServletUtil {

    private static final Logger log = LoggerFactory.getLogger(SpringServletUtil.class);

    public static HttpServletRequest getRequest() {
        try {
            return ((ServletRequestAttributes)
                    (RequestContextHolder.currentRequestAttributes())).getRequest();

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }


    public static HttpServletResponse getResponse() {
        try {
            return ((ServletRequestAttributes)
                    (RequestContextHolder.currentRequestAttributes())).getResponse();

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static HttpSession getSession() {
        HttpServletRequest request = getRequest();
        if (request != null) {
            return request.getSession();
        }
        return null;
    }
}
